package com.hanbang.taitaigou.utils.other;

import java.util.ArrayList;

import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;


public class EditHelper {

    private Context context;
    private ArrayList<TextView> editTexts = new ArrayList<TextView>();
    private ArrayList<String> hintTexts = new ArrayList<String>();
    private ArrayList<String> regex = new ArrayList<String>();

    public EditHelper(Context context) {
        this.context = context;
    }

    /*
     * 设置监听的输入框
     */
    public void setEditText(TextView... edits) {
        for (TextView e : edits) {
            editTexts.add(e);
        }

    }

    /*
     * 设置提示文本
     */
    public void setHintText(String... hints) {
        for (String e : hints) {
            hintTexts.add(e);
        }
    }

    /*
     * 设置正则的判断
     */
    public void setRegEx(String... reg) {
        for (String e : reg) {
            regex.add(e);
        }
    }


    public ArrayList<String> getText() {
        ArrayList<String> s = new ArrayList<String>();
        for (TextView e : editTexts) {
            s.add(e.getText().toString().trim());
        }
        return s;

    }

    /**
     * 获取指定的控件
     *
     * @return
     */
    public TextView getViewPostion(int postion) {
        try {
            return editTexts.get(postion);
        } catch (IndexOutOfBoundsException e) {

        }
        return null;
    }

    /*
     * 检查是否满足
     */
    public boolean check() {
        try {
            for (int i = 0; i < editTexts.size(); i++) {
                TextView e = editTexts.get(i);
                if (!e.getText().toString().trim().matches(regex.get(i))) {
                    ToastUtils.show(context, hintTexts.get(i),
                            Toast.LENGTH_LONG);
                    e.requestFocus();
                    UiUtils.shakeLeft(e, 0.85f, 6f);
                    return false;
                }

            }
            return true;
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
            return true;
        }

    }
}
